package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.rankunionwar.RankUnionWarMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_UNION_JOIN, desc = "请求加入联盟")
public class JoinUnionCmd extends AbstractCommand {

	private String unionUid;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		UnionProto.UnionJoinReqMsg reqMsg = UnionProto.UnionJoinReqMsg.parseFrom(packet.getBytes());
		unionUid = reqMsg.getUnionUid();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(player.isRequestToFast()){
			ret  =  GameErrorCode.E_REQUEST_TOO_FAST;
			return;
		}

		if(!SystemOpenMgr.systemOpen(player, eSystemId.Union.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}

		//在商会跨服判断
//		if (!StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())) {
//			ret = GameErrorCode.E_UNION_HAS_JOIN_UNION;
//			return;
//		}

		//重新加入联盟，需要判断时间
		long nowTime = System.currentTimeMillis() / 1000;
		CrossUnionMember member = CrossUnionMgr.getUnionMember("", player.getUserId());
		if(null != member && nowTime - member.getQuitTime() < GameConfig.UNION_NEXT_JOIN_NEED_TIME){
			ret = GameErrorCode.E_UNION_NEXT_JOIN_TIME_NO_ENOUGH;
			return;
		}
		UnionProto.UnionJoinGameToCrossMsg.Builder gameToCrossMsg = UnionProto.UnionJoinGameToCrossMsg.newBuilder();
		gameToCrossMsg.setUnionUid(unionUid);
		// 统兵演武加入商会限制
		if (!TongBingMgr.canChangeUnion()) {
			ret = GameErrorCode.E_TONG_BING_CAN_NOT_OPERATE_UNION;
			return;
		}
		if (!TonBingMgr.canChangeUnion()) {
			ret = GameErrorCode.E_TON_BING_CAN_NOT_OPERATE_UNION;
			return;
		}
		if (!ChannelTonBingMgr.canChangeUnion()) {
			ret = GameErrorCode.E_CTON_BING_CAN_NOT_OPERATE_UNION;
			return;
		}
		if (!FiveHeroMgr.canChangeUnion()) {
			ret = GameErrorCode.E_FIVE_HERO_CAN_NOT_OPERATE_UNION;
			return;
		}
		if(!StringUtils.isNullOrEmpty(unionUid) && !SeacraftSouthSeaMgr.canInAndOutUnion(unionUid)){
			ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_CAN_NOT_JOIN;
			return;
		}
		if(!StringUtils.isNullOrEmpty(unionUid) && !RankUnionWarMgr.canJoinUnion(unionUid)){
			ret = GameErrorCode.E_RANKUNIONWAR_ACTIVITY_CAN_NOT_JOIN;
			return;
		}
		if(!StringUtils.isNullOrEmpty(unionUid) && !PeakStrongholdActivityMgr.canChangeUnion(unionUid)){
			ret = GameErrorCode.E_PEAK_HOLD_CAN_NOT_JOIN_UNION;
			return;
		}
		if(!StringUtils.isNullOrEmpty(unionUid) && !PeakSeacraftMgr.canInAndOutUnion(unionUid)){
			ret = GameErrorCode.E_PEAK_SEACRAFT_CAN_NOT_JOIN;
			return;
		}
		//结果去重
		List<String> dataList = new ArrayList<>();
		// 海盗争霸 南海丝路禁止加入的联盟集合
		List<String> banUnionList = SeacraftSouthSeaMgr.getCurRoundPromotionUnionList();
		if(banUnionList != null){
			for(String data : banUnionList){
				if(!dataList.contains(data)){
					dataList.add(data);
				}
			}
		}

		banUnionList = PeakStrongholdActivityMgr.getEliminateUnionList();
		if(banUnionList != null){
			for(String data : banUnionList){
				if(!dataList.contains(data)){
					dataList.add(data);
				}
			}
		}

		banUnionList = PeakSeacraftMgr.getCurRoundPromotionUnionList();
		if(banUnionList != null){
            for(String data : banUnionList){
                if(!dataList.contains(data)){
                    dataList.add(data);
                }
            }
		}

		gameToCrossMsg.addAllExcludeUnionList(dataList);
		gameToCrossMsg.setPlayerBaseData(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
		player.sendPacket(Protocol.C_UNION_JOIN, gameToCrossMsg);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0) {
			UnionProto.UnionJoinRespMsg.Builder respMsg = UnionProto.UnionJoinRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_UNION_JOIN, respMsg);
		}
	}

}
